Imports Microsoft.VisualBasic



Namespace Persistencia.ModuloBodega

    Public Class ControladorPersistencia
#Region "Atributos"
        Private Shared instance As ControladorPersistencia
        Private query As String
        Private BD As Persistencia.ModuloBodega.bd
#End Region
        'Constructor Vacio
        Public Sub New()
            BD = Persistencia.ModuloBodega.bd.getInstance
        End Sub

#Region "getInstance"
        'Patron Singleton 
        Public Shared Function getInstance() As ControladorPersistencia

            If instance Is Nothing Then
                instance = New ControladorPersistencia
            End If

            Return instance

        End Function
#End Region

#Region "funciones"

#End Region

#Region "Obtener"
        Public Sub actualizarMaterialSalida(ByVal Material As MaterialSalida, ByVal ID As String)
            query = "update materialdatos  set cantidad= cantidad - " + CStr(Material.CantRetirada) + " where fkmaterial = '" + ID + "'"
            BD.EjecutarSinRetorno(query)

            query = "insert into materialregistrosalida (fkmaterial,fechasalida,retiradopor,aprobadopor,despachadopor,cantidadretirada) values ('" + ID + "','" + _
                    Material.FechaSalida + "','" + Material.RetiradoPor + "','" + Material.AprovadoPor + "','" + Material.DespachadoPor + "'," + Material.CantRetirada + ")"

            BD.EjecutarSinRetorno(query)
        End Sub

        Public Function obtenerItemOrdenCompra(ByVal numeroOC As String) As ArrayList
            Try
                Dim query As String = "select * from itemordencompra where fkordencompra=" & numeroOC
                Return BD.obteneritemOrdenCompra(query)
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Public Function ObtenerGrupo(ByVal criterio As String) As ArrayList
            Try
                Dim query As String = "select * from grupousuarios where id=" + criterio
                Return BD.ObtenerGrupo(query)
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Public Function ObtenerMaterial(ByVal fk As String) As ArrayList
            Try
                query = "select * from material where id=" + fk
                Return BD.obtenerMaterial(query)
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Public Function ObtenerFamiliaMaterial(ByVal refFamilia As String) As ArrayList
            Try
                Dim query As String = "select * from material where reffamilia = " & refFamilia
                Return BD.obtenerMaterial(query)
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Public Function ObtenerOrdenCompra(ByVal numeroOc As String) As ArrayList
            Try
                Dim query As String = "select * from ordencompra where numorden =" + numeroOc
                Return BD.obtenerOrdenCompra(query)
            Catch ex As Exception
                Throw ex
            End Try

        End Function

        Public Function ObtenerOrdenCompra() As ArrayList
            Try
                query = "select * from ordencompra"
                Return BD.obtenerOrdenCompra(query)
            Catch ex As Exception
                Throw ex
            End Try

        End Function

        Public Function ObtenerMaterial() As ArrayList
            Try
                query = "select * from material"
                Return BD.obtenerMaterial(query)
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Public Function ObtenerFamilia() As ArrayList
            Try
                query = "select * from familiamateriales"
                Return BD.ObtenerFamilia(query)
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Public Function ObtenerVisibilidadesUsuario(ByVal nombreUser As String) As Hashtable
            Try
                Dim query As String = "select fkgrupousuarios from usuariosistema where usuario ='" + nombreUser + "'"
                Return BD.EjecutarConRetorno(query)
            Catch ex As Exception
                Throw ex
            End Try
        End Function
#End Region

#Region "Insertar"
        Public Sub InsertarMaterialDatos(ByVal Mat As MaterialDatos)
            Try
                query = "insert into materialdatos (cantidad,fechaingreso,ubicacionfila,ubicacioncolumna,fkmaterial,fkbodega,fkordencompra) values (" + _
                               Mat.Cantidad + ",'" + Mat.FechaIngreso + "','" + Mat.UbicacionFila + "','" + Mat.UbicacionColumna + "','" + Mat.FkMaterial + "','" + "1" + "','" + Mat.FkOrdenCompra + "')"

                BD.EjecutarSinRetorno(query)
            Catch ex As Exception
                Throw ex
            End Try
        End Sub
#End Region

    End Class
End Namespace

